package cn.itsource.pet.service.impl;

import cn.itsource.basic.domain.Point;
import cn.itsource.basic.exception.CustomException;
import cn.itsource.basic.util.*;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.domain.SearchMasterMsgAuditLog;
import cn.itsource.pet.mapper.SearchMasterMsgAuditLogMapper;
import cn.itsource.pet.mapper.SearchMasterMsgMapper;
import cn.itsource.pet.service.ISearchMasterMsgService;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.user.domain.User;
import cn.itsource.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itsource
 * @since 2022-08-21
 */
@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private SearchMasterMsgAuditLogMapper searchMasterMsgAuditLogMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Override
    public void publish(SearchMasterMsg smm) {
        //非空校验
        if(StringUtils.isEmpty(smm.getTitle())
                || StringUtils.isEmpty(smm.getName())
                || StringUtils.isEmpty(smm.getPetType())){
            throw new CustomException("名字和类型不能为空!");
        }
        //保存寻主信息
        searchMasterMsgMapper.add(smm);
        //创建StringBuilder 来装要审核的内容
        StringBuilder sb = new StringBuilder();
        sb.append(smm.getName()).append(smm.getCoatColor()).append(smm.getAddress());
        //查询user用户 为了获取他的phone 来给它发送审核的结果的短信
        User user = userMapper.loadOne(smm.getUserId());
        //开始审核
        if(!BaiduAuditUtils.TextCensor(sb.toString())){
            //审核不通过 更新状态为-1 并向用户发送短信通知
            smm.setState(-1);
            //保存审核信息
            smm.setNote("审核不通过，存在不合法字符！");
            //发送短信告知用户
//            SendSmsUtil.SendSms(user.getPhone(),"输入的信息不合法！");
        }else{
            //审核通过 更新状态为1 并向用户发送短信通知
            smm.setState(1);
            //保存审核信息
            smm.setNote("审核通过!");
            //发送短信告知用户
//            SendSmsUtil.SendSms(user.getPhone(),"审核通过！");
            //拿到寻主的坐标 经纬度
            Point point = DistanceUtil.getPoint(smm.getAddress());
            //方圆50公里内寻找所有店铺
            List<Shop> shops = shopMapper.loadAll();
            Shop nearestShop = DistanceUtil.getNearestShop(point, shops);
            System.out.println(nearestShop.getId());
            //判断是否查询到了店铺
            if(nearestShop != null){
                smm.setShopId(nearestShop.getId());
            }
        }
        //创建日志对象
        SearchMasterMsgAuditLog log = new SearchMasterMsgAuditLog();
        //将日志的msgid和smm的id关联起来
        log.setMsgId(smm.getId());
        //添加日志的状态和时间和审核信息
        log.setNote(smm.getNote());
        log.setState(smm.getState());
        log.setAuditTime(new Date());
        //更新日志
        searchMasterMsgAuditLogMapper.add(log);
        //更新寻主信息
        searchMasterMsgMapper.update(smm);

    }
}
